package com.corx.network.core.factory;

import com.corx.network.config.properties.MqttConfig;
import com.corx.network.config.properties.NetworkConfig;
import com.corx.network.core.exception.NetworkException;
import com.corx.network.core.network.MqttNetworkClient;
import com.corx.network.core.network.Network;
import com.corx.network.core.type.NetworkTypeEnum;

/**
 * @author gx
 * @version 1.0.0
 */
public class NetworkFactory {

    private NetworkFactory() {
    }

    public static Network create(NetworkConfig config) throws NetworkException {
        if (NetworkTypeEnum.MQTT_CLIENT == config.getType()) {
            return createMqttClient((MqttConfig) config);
//      } else if (config instanceof TcpConfig) {
//            return createTcpClient((TcpConfig) config);
        }
        throw new NetworkException("不支持的配置类型: " + config.getClass().getSimpleName());
    }

    private static MqttNetworkClient createMqttClient(MqttConfig config) {
        MqttNetworkClient client = new MqttNetworkClient();
        client.setClientId(config.getKey());
        client.setBrokerUrl(config.getUrl());
        client.setUsername(config.getUsername());
        client.setPassword(config.getPassword());
        client.setAutoConnect(config.getAutoConnect());
        client.setKeepAlive(config.getKeepAlive());
        client.setCleanSession(config.getCleanSession());
        return client;
    }


}
